package com.tvunetworks.center.user.controller;

import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.json.ExcelData;
import com.tvunetworks.center.user.model.param.OwnFeatureUserParam;
import com.tvunetworks.center.user.model.vo.OwnFeatureUserRecordVo;
import com.tvunetworks.center.user.model.vo.OwnFeatureUserVo;
import com.tvunetworks.center.user.service.UserFeatureRecordService;
import com.tvunetworks.center.user.util.ExcelUtil;
import com.tvunetworks.center.user.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @program: tvucc-aggregator
 * @description: UserFeatureRecordController
 * @author lebronchen
 * @create: 2019-06-17 11:03
 **/
@RestController
@RequestMapping("/userFeatureRecord")
@Slf4j
public class UserFeatureRecordController {

    @Autowired
    private UserFeatureRecordService userFeatureRecordService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 列出拥有feature的用户
     * @param param param
     * @param pageNum pageNum
     * @param pageSize pageSize
     * @param loginUser loginUser
     * @return Result
     */
    @GetMapping("/pageOwnFeatureUser")
    public Result<Object> pageOwnFeatureUser(@Logined LoginUser loginUser, @Valid OwnFeatureUserParam param, @RequestParam(defaultValue="1") Integer pageNum, @RequestParam(defaultValue="10") Integer pageSize){
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        PageInfo<OwnFeatureUserVo> pageInfo = userFeatureRecordService.pageOwnFeatureUser(loginUser, param, pageNum, pageSize);
        return ResultUtil.success(pageInfo);
    }

    /**
     * 列出拥有feature用户 拥有feature的时间段
     * @param param param
     * @return Result
     */
    @GetMapping("/getOwnFeatureUserRecord")
    public Result<Object> getOwnFeatureUserRecord(@Valid OwnFeatureUserParam param){
        List<OwnFeatureUserRecordVo> list = userFeatureRecordService.getOwnFeatureUserRecord(param);
        return ResultUtil.success(list);
    }

    /**
     * 列出拥有feature用户 导出Excel
     * @param param param
     * @param response response
     * @param session session
     * @return Result
     */
    @GetMapping("/getOwnFeatureUserExcel")
    public Result<Object> getOwnFeatureUserExcel(HttpServletResponse response,@Valid OwnFeatureUserParam param, @RequestParam String session){
        LoginUser loginUser = (LoginUser) redisUtil.get(LocalConstant.REDIS_LOGIN_USER_KEY + session);
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        List<OwnFeatureUserVo> list = userFeatureRecordService.listOwnFeatureUser(loginUser, param);
        ExcelData excelData = new ExcelData();
        excelData.setName("License Feature Report");
        String[] titles = new String[]{"userId","email","role","userParentId","userParentEmail"};
        excelData.setTitles(titles);
        excelData.setContent(list);
        try {
            ExcelUtil.exportExcel(response,"FeatureUser",excelData);
        } catch (Exception e) {
            return ResultUtil.error("81001001","Export feature user excel fail!");
        }
        return ResultUtil.success(list);
    }
}
